package cate.game.res;

import cate.game.GameBody;
import cate.game.db.SaveConstants;
import cate.game.framework.ModuleMan;
import cate.game.res.holy_pet.HolyPetManager;
import cate.game.res.lottery.LotteryManager;
import cate.game.res.treasureturntable.TreasureTurntableManager;

import cate.game.res.furniturecall.FurnitureCallManager;
import cate.game.res.spicecall.SpiceCallManager;
import cate.game.role.res.guild.GuildManager;
import cate.game.util.IntervalCounter;

import java.util.concurrent.atomic.AtomicInteger;


public class ResManager extends ModuleMan {
    public TreasureTurntableManager astrology = new TreasureTurntableManager();
    public GuildManager guild = new GuildManager();
    public SpiceCallManager spiceCall = new SpiceCallManager();
    public FurnitureCallManager furnitureCall = new FurnitureCallManager();
    public TreasureTurntableManager treasureTurntable = new TreasureTurntableManager();

    public LotteryManager lottery = new LotteryManager();
    public HolyPetManager holyPet = new HolyPetManager();

    @Override
    public void initDependent() throws Exception {
        astrology.initDependent();
        guild.initDependent();
        spiceCall.initDependent();
        furnitureCall.initDependent();
        treasureTurntable.initDependent();
        lottery.initDependent();
        holyPet.initDependent();
    }

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        astrology.initIndependent(game);
        guild.initIndependent(game);
        spiceCall.initIndependent(game);
        furnitureCall.initIndependent(game);
        treasureTurntable.initIndependent(game);
        lottery.initIndependent(game);
        holyPet.initIndependent(game);
    }

    @Override
    public void shutdown(){
        astrology.shutdownSafe();
        guild.shutdownSafe();
        spiceCall.shutdownSafe();
        furnitureCall.shutdownSafe();
        treasureTurntable.shutdownSafe();
        lottery.shutdownSafe();
        holyPet.shutdownSafe();
    }

    @Override
    public void onDaySpan() {
        super.onDaySpan();
        guild.onDaySpanSafe();
        spiceCall.onDaySpanSafe();
    }

    @Override
    public void onWeekSpan() {
        super.onWeekSpan();
        guild.onWeekSpanSafe();
    }

    private static final AtomicInteger IDX = new AtomicInteger(0);
    private final IntervalCounter saveTrigger = new IntervalCounter(SaveConstants.RES_SAVE_SECONDS, IDX.getAndIncrement());

    public void tick(){
        if(saveTrigger.trigger()){
            long start = System.currentTimeMillis();
            shutdown();
            log.debug("game res定时保存完成, cost{} ms", System.currentTimeMillis() - start);
        }
        guild.bossRankCache.tick();
        holyPet.tick();
    }
}
